/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.boundary;

import it.polimi.meteocal.control.EventManager;
import it.polimi.meteocal.control.InvitationManager;
import it.polimi.meteocal.control.UserManager;
import it.polimi.meteocal.entity.Event;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;

/**
 *
 * @author paolo
 */
@ManagedBean(name = "homeOtherBean")
@SessionScoped
public class HomeOtherBean implements Serializable {

    @EJB
    EventManager em;

    @EJB
    UserManager um;

    @EJB
    InvitationManager im;

    private String user;
    private ScheduleEvent event = new DefaultScheduleEvent();
    private String idselected;
    private DefaultScheduleModel eventModel;

    public HomeOtherBean() {
    }

    public String getUser() {
        return user;
    }

    public String getIdselected() {
        return idselected;
    }

    public void setIdselected(String idselected) {
        this.idselected = idselected;
    }

    public ScheduleEvent getEvent() {
        return event;
    }

    public DefaultScheduleModel getEventModel() {
        return eventModel;
    }

    public void setUser(String user) {
        this.user = user;
    }
    /**
     * inizialize the schedule of a specific user(not the logged one) according to the visibility of
     * the event and the role of the logged user
     */
    public void init() {
        System.out.println("*******INIT HOME PAGE OTHER USER************" + user);
        eventModel = new DefaultScheduleModel();
        DefaultScheduleEvent scheduleEvent;
        if (!um.getUser(user).getPublicCalendar()) {
            System.out.println("CALENDARIO PRIVATO NO INFO AVAILABLE");
        } else {
            for (Event e : em.getUserEvent(user)) {
                GregorianCalendar cal = e.getEndDate();
                cal.set(Calendar.HOUR_OF_DAY, e.getEndHour().get(Calendar.HOUR_OF_DAY));
                cal.set(Calendar.MINUTE, e.getEndHour().get(Calendar.MINUTE));
                Date endDate = cal.getTime();
                cal = e.getStartDate();
                cal.set(Calendar.HOUR_OF_DAY, e.getStartHour().get(Calendar.HOUR_OF_DAY));
                cal.set(Calendar.MINUTE, e.getStartHour().get(Calendar.MINUTE));
                Date beginDate = e.getStartDate().getTime();
                if (e.getPublicEvent()) {
                    scheduleEvent = new DefaultScheduleEvent(e.getName(), beginDate, endDate, e);
                } else {
                    if (e.getAuthor().getEmail().equals(um.getLoggedUser().getEmail()) || im.invited(e, um.getLoggedUser().getEmail())) {
                        scheduleEvent = new DefaultScheduleEvent(e.getName(), beginDate, endDate, e);
                        if (e.getPublicEvent()) {
                            if (e.getOutdoor()) {
                                scheduleEvent.setStyleClass("eventpuou");
                            } else {
                                scheduleEvent.setStyleClass("eventpuin");
                            }
                        } else {
                            if (e.getOutdoor()) {
                                scheduleEvent.setStyleClass("eventprou");
                            } else {
                                scheduleEvent.setStyleClass("eventprin");
                            }
                        }

                    } else {
                        scheduleEvent = new DefaultScheduleEvent("PRIVATE EVENT", beginDate, endDate, e);
                        scheduleEvent.setStyleClass("privateevent");
                    }
                }
                eventModel.addEvent(scheduleEvent);
            }
        }

    }
/**
 * when an event is selected on the schedule the selectede event identifier is saved
 * @param selectEvent 
 */
    public void onEventSelect(SelectEvent selectEvent) {
        event = (ScheduleEvent) selectEvent.getObject();
        Event e = (Event) event.getData();
        idselected = e.getEventID().toString();
        System.out.println("***selected event: " + e.getEventID() + " " + e.getName());
    }
    /**
     *
     * @return the event detail page according to the selected event
     */
    public String gotoDetail() {
        System.out.println("*****GOTODETAIL*****" + idselected);
        return ("eventdetail?faces-redirect=true&idevent=" + idselected);
    }

}
